class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
        sort(tiles.begin(),tiles.end());
        int ans=0,cover=0,left=0;
        for(auto& tile:tiles)
        {
            int tl=tile[0],tr=tile[1];
            cover+=tr-tl+1;
            while(tiles[left][1]+carpetLen-1<tr)
            {
                cover-=tiles[left][1]-tiles[left][0]+1;
                ++left;
            }
            int uncover=max(tr-carpetLen+1-tiles[left][0],0);
            ans=max(ans,cover-uncover);
        }
        return ans;
    }
};